﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Text;

namespace HQWeb.LibarySys
{
    public partial class LibrayOutApply : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindBranchName();
                if (Request["id"] != null)
                {
                    hidPid.Value = Request["id"].ToString();
                    hidType.Value = "edit";
                    if (Request["sign"] == "2")
                    {
                        allow.Style["display"] = "";

                    }
                    string id = Request["id"];
                    BindDate(id);
                    //btnSubmit.Text = "确定";
                    //btnCancle.Visible = false;
                    btnReturn.Visible = true;
                }
                else
                {
                    applicant.Value = Session["USER_NAME"].ToString();
                }  
            }
        }

        #region 绑定分店列表
        private void BindBranchName()
        {
            BLL.SysManage.T_PUB_BRANCH LibraryBll = new BLL.SysManage.T_PUB_BRANCH();
            IList il = LibraryBll.GetModelList("");
            drop_branch.DataSource = il;
            drop_branch.DataTextField = "BRANCH_NAME"; //dropdownlist的Text的字段 
            drop_branch.DataValueField = "BRANCH_ID";//dropdownlist的Value的字段 
            drop_branch.DataBind();
            ListItem item = new ListItem("", "");
            item.Selected = true;
            drop_branch.Items.Add(item); 
        }
        #endregion
      
        #region 绑定仓库
        private void BindLibraryName(int Branch_id)
        {
            BLL.SysManage.T_PUB_LIBRARY bll = new BLL.SysManage.T_PUB_LIBRARY();
            IList librarylist = bll.GetModelByBranch_id(Branch_id);
            if (librarylist != null)
            {
                droplibrary.DataSource = librarylist;
                droplibrary.DataTextField = "LIBRARY_NAME";
                droplibrary.DataValueField = "LIBRARY_ID";
                droplibrary.DataBind();
            }
        }
        #endregion

        #region 绑定材料
        private void BindMaterial(string id)
        {
            BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL bll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
            DataTable modelList = bll.GetListByPid(id).Tables[0];
            StringBuilder strHtml = new StringBuilder();
            int count = modelList.Rows.Count;
            string strEID;
            string strMateral;
            string strMateralName;
            string strNum;

            for (int i = 0; i < count; i++)
            {
                strEID = modelList.Rows[i]["ID"].ToString();
                strMateral = modelList.Rows[i]["MATERIAL_ID"].ToString();
                strMateralName = modelList.Rows[i]["MATERIAL_NAME"].ToString();
                strNum = modelList.Rows[i]["NUM"].ToString();

                if (i != count - 1)
                {
                    txtUserIdHid.Value += strMateral + ",";
                    txtUserNameHid.Value += strMateralName + ",";
                }
                else
                {
                    txtUserIdHid.Value += strMateral;
                    txtUserNameHid.Value += strMateralName;
                }

                strHtml.Append("  <table width=\"58%\" id=\"table" + i + "\">");
                strHtml.Append(" <tr>");
                strHtml.Append("<td align=\"right\" style=\"width:10%\">");
                strHtml.Append("元素名称：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width:10%\">");
                strHtml.Append(" <input type=\"text\" readonly=\"readonly\" id=\"n" + i + "\" onchange=\"this.setAttribute('value',this.value);\" value=\"" + strMateralName + "\"/>");
                strHtml.Append("</td>");
                strHtml.Append("<td style=\"width: 10%\" align=\"right\">");
                strHtml.Append("申请数量：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width: 10%\">");
                strHtml.Append("<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" id=\"s" + i + "\" value=\"" + strNum + "\"/>");
                strHtml.Append(" </td>");
                //strHtml.Append("<td style=\"width: 10%\" align=\"right\">");
                //strHtml.Append("库存数量：");
                //strHtml.Append("</td>");
                //strHtml.Append("<td align=\"left\">");
                //strHtml.Append("<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" readonly=\"readonly\" id=\"c" + i + "\" value=\"" + strNum + "\"/>");
                //strHtml.Append(" </td>");
                strHtml.Append("<td align=\"right\" style=\"width: 10%\" >");
                strHtml.Append("<input type=\"button\" id=\"del" + i + "\" value=\"删除\"  onclick=\"DeleteNode('table" + i + "')\"/>");
                strHtml.Append(" <input type=\"text\" id=\"p" + i + "\" style=\"display: none\" value=\"" + strMateral + "\"/>");
                strHtml.Append(" <input type=\"text\" id=\"e" + i + "\" style=\"display: none\" value=\"" + strEID + "\" />");
                strHtml.Append(" <input type=\"text\" id=\"" + strMateral + "\" style=\"display: none\" value=\"table" + i + "\" />");
                strHtml.Append("</td>");
                strHtml.Append(" </tr>");
                strHtml.Append(" </table>");
            }
            hidMaterialIDList.Value = "," + txtUserIdHid.Value + ",";
            Addtable.InnerHtml = strHtml.ToString();
            hidCount.Value = count.ToString();
        }
        #endregion

        #region  绑定数据
        private void BindDate(string id)
        {
            Model.LibrarySys.T_PUB_LIBRARYOUT libraryout = new Model.LibrarySys.T_PUB_LIBRARYOUT();
            BLL.LibrarySys.T_PUB_LIBRARYOUT bll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
            libraryout = bll.GetModel(id);
            applytitel.Value = libraryout.TITEL.Trim();
            drop_branch.Value = libraryout.BRANCH_ID.ToString();
            int branchid = int.Parse(drop_branch.Value);
            BindLibraryName(branchid);
            droplibrary.Value = libraryout.LIBRARY_ID.ToString();
            applicant.Value = libraryout.APPLICANT_NAME.Trim();
            d12.Value = Convert.ToDateTime(libraryout.APPLY_TIMEOUT).ToShortDateString().Replace('/', '-');
            d13.Value = Convert.ToDateTime(libraryout.PLAN_TIMEIN).ToShortDateString().Replace('/', '-');
            Remark.Value = libraryout.REMARK.Trim();
            //Titel.Text = libraryout.TITEL;
            if (Request["sign"] == "2")
            {
                if (libraryout.NOTATION != null)
                {
                    Notation.Text = libraryout.NOTATION;
                }
                if (libraryout.APPROVER_NAME != null)
                {
                    Approver.Text = libraryout.APPROVER_NAME;
                }
            }
            BindMaterial(id);
        }
        #endregion

        protected void btnCancle_Click(object sender, EventArgs e)
        {

            BindBranchName();
            d12.Value = "";
            d13.Value = "";
            Remark.Value = "";
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("ApplyList.aspx");
        }
    }
}